package com.kkshop.gwt.login;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.kkshop.gwt.i18n.ApplicationResources;

public class LoginView {
    
    private ApplicationResources resources = ApplicationResources.ApplicationResourcesHelper.getInstance().getResources();

    private FormPanel form;
    private HTML messageAreaHtml;
    private Label usernameLabel;
    private TextBox usernameText;
    private Label passwordLabel;
    private PasswordTextBox passwordText;
    private CheckBox remeberMeCheck;
    private Button submitButton;

    public LoginView() {
        FlexTable table = new FlexTable();
        table.setWidget(0, 0, getMessageAreaHtml());
        table.setWidget(1, 0, getUsernameLabel());
        table.setWidget(1, 1, getUsernameText());
        table.setWidget(2, 0, getPasswordLabel());
        table.setWidget(2, 1, getPasswordText());
        table.setWidget(3, 1, getRemeberMeCheck());
        table.setWidget(4, 1, getSubmitButton());
        
        table.getFlexCellFormatter().setColSpan(0, 0, 2);
        
        FormPanel form = getForm();
        form.setWidget(table);
    }
    
    public Widget getWidget() {
        return getForm();
    }
    
    protected FormPanel getForm() {
        if(form == null) {
            form = new FormPanel();
            form.setMethod(FormPanel.METHOD_POST);
            form.setAction("/j_ajax_acegi_security_check");
        }
        return form;
    }
    
    protected HTML getMessageAreaHtml() {
        if(messageAreaHtml == null) {
            messageAreaHtml = new HTML();
        }
        return messageAreaHtml;
    }

    protected Label getUsernameLabel() {
        if(usernameLabel == null) {
            usernameLabel = new Label(resources.loginUsername() + ":");
        }
        return usernameLabel;
    }

    protected TextBox getUsernameText() {
        if(usernameText == null) {
            usernameText = new TextBox();
            usernameText.setName("j_username");
        }
        return usernameText;
    }

    protected Label getPasswordLabel() {
        if(passwordLabel == null) {
            passwordLabel = new Label(resources.loginPassword() + ":");
        }
        return passwordLabel;
    }

    protected PasswordTextBox getPasswordText() {
        if(passwordText == null) {
            passwordText = new PasswordTextBox();
            passwordText.setName("j_password");
        }
        return passwordText;
    }

    protected CheckBox getRemeberMeCheck() {
        if(remeberMeCheck == null) {
            remeberMeCheck = new CheckBox(resources.loginRememberMe());
            remeberMeCheck.setName("_acegi_security_remember_me");
        }
        return remeberMeCheck;
    }

    protected Button getSubmitButton() {
        if(submitButton == null) {
            submitButton = new Button(resources.loginSubmit());
        }
        return submitButton;
    }
}
